home *** CD-ROM | disk | FTP | other *** search
/ Computer Music Interactif…cial Edition 1999 Winter / cd 3.iso / mac / Mac / Shares / Midishare™1.68 / Development Tools / Libraries / MidiFiles / example.c next >
Encoding:
Text File  |  1995-05-17  |  2.3 KB  |  76 lines  |  [TEXT/MPS ]

  1.  
  2. /*--------------------------------------------------------------------------
  3.  *
  4.  * a typical example of code to read a MIDI file might be 
  5.  * the following one. 
  6.  *
  7.  * Warning! take care of that this code doesn't check for errors
  8.  *
  9.  *--------------------------------------------------------------------------*/
  10. MidiSeqPtr ReadMidiFile( char *itsName)
  11. {
  12.     MidiSeqPtr seq, tmp;
  13.     midiFILE *fd;
  14.     unsigned short n;
  15.     
  16.     seq= MidiNewSeq()            /* allocate a new MidiShare sequence */
  17.     if( fd= MidiFileOpen( itsName, MidiFileRead))
  18.     {
  19.         n= fd->ntrks;                        /* get the number of tracks */
  20.         while( n--) {
  21.             tmp= MidiFileReadTrack( fd);    /* read every track            */
  22.  
  23.             Mix( tmp, seq);        /* the Mix function is to provide        */
  24.                                 /* it transfers the content of the first*/
  25.                                 /* sequence to the second one,             */
  26.                                 /* its interface might be :                */
  27.                             /* void Mix( MidiSeqPtr src, MidiSeqPtr dst)*/
  28.  
  29.             MidiFreeSeq( tmp);    /* this sequence is now empty, we can    */
  30.                                 /* free it without freing the readed    */
  31.                                 /* events                                */
  32.         }
  33.         MidiFileClose( fd);
  34.     }
  35.     return seq;
  36. }
  37.  
  38.  
  39. /*--------------------------------------------------------------------------
  40.  *
  41.  * a typical example of code to create a format 1 MIDI file might be 
  42.  * the following one. 
  43.  *
  44.  * Warning again! take care of that this code doesn't check for errors
  45.  *
  46.  *--------------------------------------------------------------------------*/
  47. void WriteMIDIFile( char *itsName)
  48. {
  49.     midiFILE *fd;
  50.  
  51.     /* we first create a new MIDI file using a format 1    */
  52.     if( fd= MidiFileCreate( itsName, midifile1, TicksPerQuarterNote, 500))
  53.     {
  54.         /* for the file consistency, the first track     */
  55.         /* to write is the tempo map                    */                
  56.         MidiFileWriteTrack( fd, myTempoMap);
  57.  
  58.         /* then we can write all the other tracks        */
  59.                         /* 
  60.                            it is the program responsibility to determine 
  61.                            the content of the tracks. Here, every track is
  62.                            stored in separate MidiShare sequences (myTempoMap,
  63.                            track1, track2,...trackn). They are supposed to be
  64.                            global variables. Of course, events in the file will
  65.                            keep exactly the same order than in the sequence
  66.                          */
  67.         MidiFileWriteTrack( fd, track1);
  68.         MidiFileWriteTrack( fd, track2);
  69.         /* ...*/
  70.         MidiFileWriteTrack( fd, trackn);
  71.         
  72.         /* and we finaly close the file                    */
  73.         MidiFileClose( fd);
  74.     }
  75. }
  76.